package org.qdao.core;

import java.sql.DatabaseMetaData;

public class DBType {

	private int type = DB_TYPE_UNKNOWN;

	public static final int DB_TYPE_UNKNOWN = -1;

	public static final int DB_TYPE_ORACLE = 1;

	public static final int DB_TYPE_MYSQL = 2;

	public static final int DB_TYPE_SYMFOWARE = 3;

	public static final int DB_TYPE_HIRDB = 4;

	public static final int DB_TYPE_SYBASE = 5;

	public static final int DB_TYPE_DERBY = 6;

	public static final int DB_TYPE_SQLITE = 7;

	public static final int DB_TYPE_H2 = 8;

	public static final int DB_TYPE_POSTGRESQL = 9;

	public static final int DB_TYPE_DB2 = 10;

	public static final int DB_TYPE_SQLSERVER = 11;

	public static final int DB_TYPE_HSQLDB = 12;

	public static final int DB_TYPE_INTERBASE = 13;

	public DBType() {
		super();
	}

	/**
	 * set the db type
	 * 
	 * @param name
	 *            2010/01/29 16:22:06
	 */
	public void setType(String driver) {
		final String DB_ORACLE = "oracle"; //$NON-NLS-1$
		final String DB_SYMFOWARE = "symfoware"; //$NON-NLS-1$
		final String DB_MYSQL = "mysql"; // com.mysql.jdbc.Driver
		final String DB_HIRDB = "hirdb"; // JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDriver
		final String DB_SYBASE = "sybase"; // com.sybase.jdbc.SybDriver
		final String DB_DERBY = "derby"; // :org.apache.derby.jdbc.ClientDriver
		final String DB_SQLITE = "sqlite"; // //$NON-NLS-1$
		final String DB_H2 = "h2"; // org.h2.Driver //$NON-NLS-1$
		final String DB_POSTGRESQL = "postgresql"; // org.postgresql.Driver
		final String DB_DB2 = "db2"; // com.ibm.db2.jcc.DB2Driver
		final String DB_SQLSERVER = "sqlserver"; // com.microsoft.jdbc.sqlserver.SQLServerDriver,
		final String DB_HSQLDB = "hsqldb"; // org.hsqldb.jdbcDriver
		final String DB_INTERBASE = "interbase"; //$NON-NLS-1$

		String driverName = driver.toLowerCase();

		if (driverName.indexOf(DB_ORACLE) >= 0) {
			type = DB_TYPE_ORACLE;
		} else if (driverName.indexOf(DB_SYMFOWARE) >= 0) {
			type = DB_TYPE_SYMFOWARE;
		} else if (driverName.indexOf(DB_MYSQL) >= 0) {
			type = DB_TYPE_MYSQL;
		} else if (driverName.indexOf(DB_HIRDB) >= 0) {
			type = DB_TYPE_HIRDB;
		} else if (driverName.indexOf(DB_SYBASE) >= 0) {
			type = DB_TYPE_SYBASE;
		} else if (driverName.indexOf(DB_DERBY) >= 0) {
			type = DB_TYPE_DERBY;
		} else if (driverName.indexOf(DB_SQLITE) >= 0) {
			type = DB_TYPE_SQLITE;
		} else if (driverName.indexOf(DB_H2) >= 0) {
			type = DB_TYPE_H2;
		} else if (driverName.indexOf(DB_POSTGRESQL) >= 0) {
			type = DB_TYPE_POSTGRESQL;
		} else if (driverName.indexOf(DB_DB2) >= 0) {
			type = DB_TYPE_DB2;
		} else if (driverName.indexOf(DB_SQLSERVER) >= 0) {
			type = DB_TYPE_SQLSERVER;
		} else if (driverName.indexOf(DB_HSQLDB) >= 0) {
			type = DB_TYPE_HSQLDB;
		} else if (driverName.indexOf(DB_INTERBASE) >= 0) {
			type = DB_TYPE_INTERBASE;
		} else {
			type = DB_TYPE_UNKNOWN;
		}

	}

	public static int getType(String driver) {
		final String DB_ORACLE = "oracle"; //$NON-NLS-1$
		final String DB_SYMFOWARE = "symfoware"; //$NON-NLS-1$
		final String DB_MYSQL = "mysql"; // com.mysql.jdbc.Driver
		final String DB_HIRDB = "hirdb"; // JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDriver
		final String DB_SYBASE = "sybase"; // com.sybase.jdbc.SybDriver
		final String DB_DERBY = "derby"; // :org.apache.derby.jdbc.ClientDriver
		final String DB_SQLITE = "sqlite"; // //$NON-NLS-1$
		final String DB_H2 = "h2"; // org.h2.Driver //$NON-NLS-1$
		final String DB_POSTGRESQL = "postgresql"; // org.postgresql.Driver
		final String DB_DB2 = "db2"; // com.ibm.db2.jcc.DB2Driver
		final String DB_SQLSERVER = "sqlserver"; // com.microsoft.jdbc.sqlserver.SQLServerDriver,
		final String DB_HSQLDB = "hsqldb"; // org.hsqldb.jdbcDriver
		final String DB_INTERBASE = "interbase"; //$NON-NLS-1$

		String driverName = driver.toLowerCase();

		if (driverName.indexOf(DB_ORACLE) >= 0) {
			return DB_TYPE_ORACLE;
		} else if (driverName.indexOf(DB_SYMFOWARE) >= 0) {
			return DB_TYPE_SYMFOWARE;
		} else if (driverName.indexOf(DB_MYSQL) >= 0) {
			return DB_TYPE_MYSQL;
		} else if (driverName.indexOf(DB_HIRDB) >= 0) {
			return DB_TYPE_HIRDB;
		} else if (driverName.indexOf(DB_SYBASE) >= 0) {
			return DB_TYPE_SYBASE;
		} else if (driverName.indexOf(DB_DERBY) >= 0) {
			return DB_TYPE_DERBY;
		} else if (driverName.indexOf(DB_SQLITE) >= 0) {
			return DB_TYPE_SQLITE;
		} else if (driverName.indexOf(DB_H2) >= 0) {
			return DB_TYPE_H2;
		} else if (driverName.indexOf(DB_POSTGRESQL) >= 0) {
			return DB_TYPE_POSTGRESQL;
		} else if (driverName.indexOf(DB_DB2) >= 0) {
			return DB_TYPE_DB2;
		} else if (driverName.indexOf(DB_SQLSERVER) >= 0) {
			return DB_TYPE_SQLSERVER;
		} else if (driverName.indexOf(DB_HSQLDB) >= 0) {
			return DB_TYPE_HSQLDB;
		} else if (driverName.indexOf(DB_INTERBASE) >= 0) {
			return DB_TYPE_INTERBASE;
		} else {
			return DB_TYPE_UNKNOWN;
		}
	}

	/**
	 * judge whether the url support
	 * 
	 * @param type
	 * @param url
	 * @return 2010/02/04 15:36:08
	 */
	public static boolean supportUrl(int type, String url) {
		if (type == DB_TYPE_ORACLE) {
			return url.toLowerCase().matches("^jdbc:oracle:thin:@(.+):(\\d+):(.+)");
		} else if (type == DB_TYPE_MYSQL) {
			return url.toLowerCase().matches("^jdbc:mysql://(.+):(\\d+)/(.+)");
		} else {
			return false;
		}
	}

	/**
	 * return the type
	 * 
	 * @param name
	 * @return 2010/01/29 16:22:16
	 */
	public int getType() {
		return this.type;
	}

	public int getType(DatabaseMetaData meta) {
		try {
			setType(meta.getDriverName());
		} catch (Exception e) {
		}
		return this.type;
	}

	/**
	 * set the type by the DatabaseMetaData
	 * 
	 * @param objMet
	 *            2010/01/29 16:23:22
	 */
	public void setType(DatabaseMetaData meta) {
		try {
			setType(meta.getDriverName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 *            2010/01/29 16:13:31
	 */
	public static void main(String[] args) {
	}
}
